package org.lg.dao.impl;

import org.lg.config.Repository;
import org.lg.config.Resource;
import org.lg.dao.AccoutDao;
import org.lg.dao.TeacherDao;
import org.lg.entity.Teacher;
import org.lg.entity.TeacherExample;
import org.lg.mapper.TeacherMapper;
import org.lg.page.PageInfo;
import org.lg.vo.Account;

import java.util.List;
import java.util.Optional;

@Repository("teacherDao")
public class TeacherDaoImpl implements TeacherDao, AccoutDao {
    @Resource
    TeacherMapper teacherMapper;
    @Override
    public List<Teacher> getAllTeachers() throws Exception {
//        List<Teacher> teachers = Arrays.asList(
//        new Teacher[]{new Teacher("0089","男"),new Teacher("0049","女"),new Teacher("0087","男")});

        return teacherMapper.selectByExample(null);
    }

    /**
     * 分页查询学生
     * @param pageInfo
     * @return
     */

    public List<Teacher> getTeacherByPage(PageInfo pageInfo){
        TeacherExample teacherExample = new TeacherExample();
        long l = teacherMapper.countByExample(null);
        pageInfo.setItemCount((int)l);
        teacherExample.setOffset(pageInfo.getOffset());
        teacherExample.setLimit(pageInfo.getPageSize());
        return teacherMapper.selectByExample(teacherExample);
    }

    /**
     * 查询优秀教师
     * @param
     * @return
     */

    public List<Teacher> getTop6Teacher(){
        TeacherExample teacherExample = new TeacherExample();                   teacherExample.setLimit(6);
        teacherExample.setOrderByClause("birthday");
        return teacherMapper.selectByExample(teacherExample);
    }

    @Override
    public Teacher getTeacherByTno(String tno) throws Exception {
        return null;
    }

    @Override
    public Integer addTeacher(Teacher teacher) throws Exception {
        return null;
    }


    @Override
    public Account getAccountByUsername(String username) throws Exception {
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andTnameEqualTo(username);
        List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
        Optional<List<Teacher>> op = Optional.ofNullable(teachers);
        return op.isPresent()&&op.get().size()>0?op.get().get(0):null;
    }
}
